/////////////////////////////////////////////////////////////////
// Script:  slcicp storage script
//          - provides a simple data storage for the cicp prim 
//			- mainly used to hold data over a reset
//
// by Jeko Snook
// Glasshouse by Green Phospor
/////////////////////////////////////////////////////////////////

// constants for prim internal communication
integer     CMD_STORAGE			= 256; // prefix for specifying the message as storage communication
integer     CMD_STORAGE_READ    = 512; // reads a value and returns it
integer     CMD_STORAGE_WRITE	= 1024; // add data to storage 
integer		CMD_RESPONSE		= 2048; // used to specify a response
integer		CMD_RESET			= 128; // reset script

/////////////////////////////////////////////////////////////////
// global vars
/////////////////////////////////////////////////////////////////

list	data					= []; // contains all data as a strided list of [dataIdentifier, data]

/////////////////////////////////////////////////////////////////
//  states: default
/////////////////////////////////////////////////////////////////

 default
 {
     link_message(integer linkNumber, integer number, string data, key id)
     {
         if (number & CMD_STORAGE && !(number & CMD_RESPONSE)) {
             list parameters = llCSV2List(data);
             if (number & CMD_STORAGE_WRITE) {
                 data += [(string)id, data];
             }
             else if (number & CMD_STORAGE_READ) {
                 integer varPos = llListFindList(llList2ListStrided(data, 0, -1, 2), (list)id);
                 string dataString = "";
                 if (~varPos) {
                     varPos = (varPos << 1) + 1;
                     string dataString = llList2String(config, varPos);
                 }
                 llMessageLinked(LINK_THIS, CMD_STORAGE | CMD_RESPONSE | CMD_STORAGE_READ, dataString, id);
             }
             else if (number & CMD_RESET) {
                 llResetScript();
             }
         }
     }
 }
